import { NextRequest, NextResponse } from 'next/server'
import { requireAuth, withErrorHandling } from '@/lib/auth-middleware'
import { terminateAllSessions } from '@/lib/auth-service'

export const DELETE = withErrorHandling(async (request: NextRequest) => {
  return requireAuth(request, async (req) => {
    try {
      const token = request.headers.get('authorization')?.replace('Bearer ', '') || ''
      const count = await terminateAllSessions(req.user!.id, token)
      
      return NextResponse.json({
        success: true,
        data: count,
        message: `成功终止 ${count} 个会话`
      })
    } catch (error) {
      console.error('终止所有会话失败:', error)
      return NextResponse.json({
        success: false,
        error: '终止所有会话失败'
      }, { status: 500 })
    }
  })
})